//express引入后是一个方法需要调用，与fs不同。
//引入模块并调用
const express = require("express")();
const mysql = require("mysql");
//文件路径
const serve = require("express-static")
//端口
const duankou = 8088;

// Node解决跨域问题
express.all("/*", function(req, res, next) {
    // 跨域处理
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", ' 3.2.1');
    res.header("Content-Type", "application/json;charset=utf-8");
    next(); // 执行下一个路由
})

//规划链接
let sql = mysql.createConnection({
    host : "localhost",
    user : "root",
    password : "123456",
    database : "tech",
    timezone:"08:00",
})

//尝试连接
sql.connect()


//监听登录浏览器请求
express.get("/a",function(request,response){
    let urename = request.query.urename;
    let password = request.query.password;
    sql.query(`SELECT * FROM yonghu WHERE name = "${urename}" AND password = "${password}"`,function(error,data){
        if(error){
            response.send("cuo")
        }
        if(data){
            //给浏览器返回内容
            if(data.length){
                response.send("dui");
            }else{
                response.send("cuo");
            }
        }
    })
})

//监听注册浏览器请求
express.get("/n",function(request,response){
    let name = request.query.name
    let word = request.query.word
    sql.query(`SELECT * FROM yonghu WHERE name = "${name}"`,function(error,data){
        if(error){
            response.send("cuo")
        }
        if(data){
            if(data.length){
                response.send("cuo");
            }else{
                sql.query(`INSERT INTO yonghu (name,password) VALUES ("${name}","${word}")`,function(error){
                    if(error){
                        response.send("cuo")
                    }else{
                        response.send("dui")
                    }
                })
            }
        }
    })
})

//监听首页请求
express.get("/shou",function(request,response){
    sql.query('SELECT * FROM shouye',(aerr,data)=>{
        response.send(data)
    })
})

//监听登录用户首页
express.get("/dshou",function(request,response){
    sql.query('SELECT * FROM shouye',(aerr,data)=>{
        response.send(data)
    })
})

//监听商品详情页请求
express.get("/xiang",function(request,response){
    let id = request.query.id;
    sql.query(`SELECT * FROM shangpinxinxi WHERE id=${id}`,(aerr,data)=>{
        response.send(data)
    })
})

//详情页加入购物车请求
express.get("/cun",function(request,response){
    let id = request.query.id;
    let name = request.query.name;
    let jiage = request.query.jiage;
    let yonghu = request.query.yonghu;
    let shangpinimg = request.query.shangpinimg;
    sql.query(`INSERT INTO gouwuche (shangpin,name,jiage,yonghu,shangpinimg) VALUES ("${id}","${name}","${jiage}","${yonghu}","${shangpinimg}")`,function(error){
        if(error){
            response.send("cuo")
        }else{
            response.send("dui")
        }
    })
})

//购物车
express.get("/gou",function(request,response){
    let yonghu = request.query.yonghu;
    sql.query(`SELECT gouwuche.id,gouwuche.shangpin,gouwuche.name,gouwuche.jiage,gouwuche.shangpinimg FROM gouwuche,yonghu,shangpinxinxi WHERE gouwuche.yonghu="${yonghu}" AND yonghu.name=gouwuche.yonghu AND gouwuche.shangpin=shangpinxinxi.id`,(aerr,data)=>{
        response.send(data)
    })
})

//购物车删除
express.get("/goushan",function(request,response){
    let id = request.query.id;
    sql.query(`DELETE FROM gouwuche WHERE id="${id}"`,(aerr)=>{
        if(!aerr){
            response.send("shan")
        }
    })
})

//账户信息
express.get("/zhanghao",function(request,response){
    let name = request.query.name;
    sql.query(`SELECT * FROM yonghu WHERE name = "${name}"`,function(error,data){
        //给浏览器返回内容
        response.send(data)
    })
})
//账户信息修改
express.get("/zhangxiugai",function(request,response){
    let xinxi = request.query.xinxi;
    let name = request.query.name;
    sql.query(`UPDATE yonghu SET dizhi="${xinxi}" WHERE name="${name}"`,function(error){
        //给浏览器返回内容
        response.send(!error)
    })
})








//监听表中信息请求
express.get("/xinxi",(request,response)=>{
    sql.query('SELECT * FROM kehuxinxi ORDER BY id',(aerr,data)=>{
        response.send(data)
    })
})

//监听插入请求
express.get("/cha",(request,response)=>{
    let name = request.query.name;
    let sex = request.query.sex;
    let age = request.query.age;
    let jiguan = request.query.jiguan;
    let time = request.query.time;
    if(name&&sex&&age&&jiguan&&time){
        sql.query(`INSERT INTO kehuxinxi (name,sex,age,jiguan,time) VALUES ("${name}","${sex}","${age}","${jiguan}","${time}")`,(aerr)=>{
            if(!aerr){
                response.send("dui")
            }
        })

    }
})

//删除监听
express.get("/shan",(request,response)=>{
    let id = request.query.id;
    if(id){
        sql.query(`DELETE FROM kehuxinxi WHERE id="${id}"`,(aerr)=>{
            if(!aerr){
                response.send("shan");
            }
        })
    }

})

//监听插入请求
express.get("/geng",(request,response)=>{
    let id = request.query.id;
    let name = request.query.name;
    let sex = request.query.sex;
    let age = request.query.age;
    let jiguan = request.query.jiguan;
    let time = request.query.time;
    if(name&&sex&&age&&jiguan&&time){
        sql.query(`UPDATE kehuxinxi SET name="${name}",sex="${sex}",age="${age}",jiguan="${jiguan}",time="${time}" WHERE id="${id}"`,(aerr)=>{
            if(!aerr){
                response.send("dui")
            }
        })

    }
})







//不同的文件路径
express.use(serve(__dirname + '/inhtml'));


//监听端口
express.listen(duankou)
//返回到后台控制台开放的端口
console.log("server is running at " + duankou)